package com.cal.utms.action.dealing;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.dealing.DealDao;
import com.cal.utms.dao.user_maintenance.UserDaoImpl;
import com.cal.utms.dto.dealing.FdDealDto;
import com.cal.utms.entity.Deal;
import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.entity.User;
import com.cal.utms.model.dealing.FdDealModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class FdDealAction extends ActionSupport implements ModelDriven<FdDealDto> {

	private static final long serialVersionUID = 1L;
	FdDealDto fdDealDto = new FdDealDto();

	@Override
	public FdDealDto getModel() {
		return fdDealDto;
	}

	public String queryFd() {
		DealDao dealDao = new DealDao();
		if (fdDealDto.getQueryOne() != null && !fdDealDto.getQueryOne().equals("")) {
			fdDealDto.setDeals(dealDao.queryDeals("FD", fdDealDto.getQueryOne()));
		} else {
			fdDealDto.setDeals(dealDao.queryDeals("FD"));
		}
		fdDealDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String queryDeal() {
		DealDao dealDao = new DealDao();
		FdDealModel fdDealModel = new FdDealModel();
		// dealDto.setDeal(dealDao.getById(dealDto.getId()));
		Deal deal = dealDao.getById(fdDealDto.getId());
		if (deal != null) {
			fdDealDto.setData(fdDealModel.mapFdToDto(deal));
			fdDealDto.setSuccess(true);
		} else {
			fdDealDto.setSuccess(false);
		}
		return Action.SUCCESS;
	}

	public String saveFd() {
		FdDealModel fdDealModel = new FdDealModel();
		fdDealDto.setSuccess(true);

		Deal deal = fdDealModel.creatFdDeal(fdDealDto);
		DealDao dealDao = new DealDao();

		if (deal.getRolloverShortmaturity() != null && !deal.getRolloverShortmaturity().trim().equals("")) {
			Deal prevDeal = dealDao.getByCode(deal.getRolloverShortmaturity());
			if (prevDeal == null) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Cannot Find Previous Deal");
				return Action.SUCCESS;
			} else if (deal.getFund().getId() != prevDeal.getFund().getId()) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Funds Mismatch. RollOver Failed!");
				return Action.SUCCESS;
			} else if (deal.getValueDate().compareTo(prevDeal.getMaturityDate()) < 0) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Invalid Value Date. RollOver Failed!");
				return Action.SUCCESS;
			} else if (deal.getIssuer().getId() != prevDeal.getIssuer().getId()) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Invalid Issuer. RollOver Failed!");
				return Action.SUCCESS;
			} else {
				prevDeal.setRolloverShortmaturity(deal.getDealCode());
				deal.setDeal(prevDeal);
			}
		}
		deal.setDayBasis("Act / 365");
		if (fdDealDto.isSuccess()) {
			dealDao.addFd(deal, fdDealDto);
		}
		fdDealDto.setDealCode(deal.getDealCode());
		return Action.SUCCESS;
	}

	public String updateFd() {
		fdDealDto.setSuccess(true);
		if (fdDealDto.getDealCode() == null) {
			fdDealDto.setStatus("Empty Deal Code");
			fdDealDto.setSuccess(false);
			return Action.SUCCESS;
		} else {
			DealDao dealDao = new DealDao();
			Deal deal = dealDao.getByCode(fdDealDto.getDealCode());
			if (deal == null) {
				fdDealDto.setStatus("Invalid Deal Code");
				fdDealDto.setSuccess(false);
				return Action.SUCCESS;
			} else {
				deal = new FdDealModel().updateFdDeal(deal, fdDealDto);
				if (fdDealDto.isSuccess()) {
					dealDao.update(deal);
					fdDealDto.setSuccess(true);
					fdDealDto.setDealCode(deal.getDealCode());
				} else {
					fdDealDto.setSuccess(false);
				}
			}
		}
		return Action.SUCCESS;
	}

	public String rejectFdDeal() {
		if (fdDealDto.getDealCode() == null) {
			fdDealDto.setStatus("Empty Deal Code!");
			fdDealDto.setSuccess(false);
			return Action.SUCCESS;
		}
		DealDao dealDao = new DealDao();
		Deal deal = dealDao.getByCode(fdDealDto.getDealCode());
		if (deal == null) {
			fdDealDto.setStatus("Invalid Deal Code!");
			fdDealDto.setSuccess(false);
			return Action.SUCCESS;
		}
		deal.setStatus("Rejected");
		deal.setModifiedDate(new Date());
		deal.setModifiedUser(fdDealDto.getUser());
		dealDao.update(deal);
		if (deal.getRolloverShortmaturity() != null && !deal.getRolloverShortmaturity().trim().equals("")) {
			Deal prevDeal = dealDao.getByCode(deal.getRolloverShortmaturity());
			if (prevDeal != null) {
				prevDeal.setRolloverShortmaturity(null);
				dealDao.update(prevDeal);
			}
		}
		fdDealDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String confirmFd() {
		DealDao dealDao = new DealDao();
		fdDealDto.setSuccess(true);

		if (fdDealDto.getDealCode() == null) {
			fdDealDto.setSuccess(false);
			fdDealDto.setStatus("Cannot Find Deal Code!");
		}
		Deal oldDeal = dealDao.getByCode(fdDealDto.getDealCode());
		if (oldDeal == null) {
			fdDealDto.setSuccess(false);
			fdDealDto.setStatus("InValid Deal Code!");
			return Action.SUCCESS;
		}

		if (oldDeal.getCreatedUser().equals(fdDealDto.getUser())) {
			fdDealDto.setSuccess(false);
			fdDealDto.setStatus("Created User cannot confirm!");
			return Action.SUCCESS;
		}

		FdDealModel fdDealModel = new FdDealModel();
		Deal deal = fdDealModel.confFdDeal(fdDealDto);

		if (deal.getRolloverShortmaturity() != null && !deal.getRolloverShortmaturity().trim().equals("")) {
			Deal prevDeal = dealDao.getByCode(deal.getRolloverShortmaturity());
			if (prevDeal == null) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Cannot Find Previous Deal");
				return Action.SUCCESS;
			} else if (deal.getFund().getId() != prevDeal.getFund().getId()) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Funds Mismatch. RollOver Failed!");
				return Action.SUCCESS;
			} else if (deal.getValueDate().compareTo(prevDeal.getMaturityDate()) < 0) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Invalid Value Date. RollOver Failed!");
				return Action.SUCCESS;
			} else if (deal.getIssuer().getId() != prevDeal.getIssuer().getId()) {
				fdDealDto.setSuccess(false);
				fdDealDto.setStatus("Invalid Issuer. RollOver Failed!");
				return Action.SUCCESS;
			} else {
				prevDeal.setRolloverShortmaturity(deal.getDealCode());
				deal.setDeal(prevDeal);
				deal.setRolloverShortmaturity(null);
			}
		}
		if (oldDeal.equals(deal)) {
			List<MoneyTransaction> moneyTransactions = new ArrayList<MoneyTransaction>();
			Date today = new Date();
			MoneyTransaction creditEntry = new MoneyTransaction();
			MoneyTransaction debitEntry = new MoneyTransaction();
			User user = new UserDaoImpl().getUserByUsername(LoginAction.getCurrentLogingUser().getUserName());

			creditEntry.setCreditDebit("C");
			if (oldDeal.getIssuer() != null) {
				creditEntry.setCustomerType("ISU");
				creditEntry.setTranTo(oldDeal.getIssuer().getId());
			} else {
				fdDealDto.setStatus("Cannot Find Issuer for Money Transactions!");
				fdDealDto.setSuccess(false);
				return Action.SUCCESS;
			}
			creditEntry.setDate(today);
			creditEntry.setEffectDate(oldDeal.getValueDate());
			creditEntry.setFund(oldDeal.getFund());
			creditEntry.setTranAmont(oldDeal.getDealValue());
			creditEntry.setTranType("DEAL");
			creditEntry.setTranTypeId(oldDeal.getId());
			creditEntry.setUser(user);

			debitEntry.setCreditDebit("D");
			if (oldDeal.getFund() != null) {
				debitEntry.setCustomerType("FUN");
				debitEntry.setTranTo(oldDeal.getFund().getId());
			} else {
				fdDealDto.setStatus("Cannot Find Fund for Money Transactions!");
				fdDealDto.setSuccess(false);
				return Action.SUCCESS;
			}
			debitEntry.setDate(today);
			debitEntry.setEffectDate(oldDeal.getValueDate());
			debitEntry.setFund(oldDeal.getFund());
			debitEntry.setTranAmont(oldDeal.getDealValue().multiply(new BigDecimal(-1)));
			debitEntry.setTranType("DEAL");
			debitEntry.setTranTypeId(oldDeal.getId());
			creditEntry.setUser(user);

			moneyTransactions.add(creditEntry);
			moneyTransactions.add(debitEntry);

			oldDeal.setStatus("Confirmed");
			oldDeal.setConfirmedUser(fdDealDto.getUser());
			oldDeal.setConfirmedDate(new Date());
			dealDao.confirm(oldDeal, moneyTransactions);
			fdDealDto.setStatus("OK");
			fdDealDto.setSuccess(true);
		} else {
			fdDealDto.setStatus("Deal Mismatch!");
			fdDealDto.setSuccess(false);
		}

		return Action.SUCCESS;
	}

	public String valuateDeal() {
		FdDealModel fdDealModel = new FdDealModel();
		fdDealDto.setSuccess(true);
		fdDealModel.valuate(fdDealDto);
		return Action.SUCCESS;
	}

}
